<template>
    <ych-modal
      :fullscreen="false"
      size="middle"
      :title="title"
      :visible="visible"
      :confirmLoading="spinning > 0"
      @ok="handleOk"
      @cancel="handleCancel"
      :cancelText="$t('m.common.button.close')">
      <a-spin :spinning="spinning > 0">
        <vxe-table
          header-align="center"
          border
          :scroll-y="{enabled: false}"
          ref="xTable"
          class="sortable-row-vxe"
          show-header-overflow
          show-overflow
          :row-config="{isHover: true, useKey: true, keyField: 'id'}"
          :data="dataSource">
          <vxe-column width="60" align="center">
            <template #default>
                <span class="drag-btn">
                  <i class="vxe-icon-num-list"></i>
                </span>
            </template>
          </vxe-column>
          <vxe-column type="checkbox" width="50" align="center"></vxe-column>
          <vxe-column type="seq" :title="$t('m.common.label.number')" width="50" align="center"></vxe-column>
          <vxe-column field="colCode" :title="$t('m.eform.apptable.entity.colCode')"  :min-width="150"></vxe-column>
          <vxe-column field="colName" :title="$t('m.eform.apptable.entity.colName')"  :min-width="150"></vxe-column>
        </vxe-table>
      </a-spin>
    </ych-modal>
</template>

<script>
  import {YchVxeTableMixin} from '../../../../mixins/YchVxeTableMixin'
  import {randomString} from "../../../../utils/util";
  export default {
    name: 'SelectSystemField',
    mixins: [YchVxeTableMixin],
    data() {
      return {
        dataSource: [],
        oldDataSource:[]
      }
    },
    methods: {
      add(dataSource){
        this.oldDataSource = dataSource
        const dataStatus = this.createDefaultRow('data_status', '草稿状态', 'varchar', 36, false, '')
        const logicStatus = this.createDefaultRow('logic_status', '逻辑删除状态', 'varchar', 36, false, '')
        this.dataSource = [dataStatus, logicStatus]
        this.visible = true
      },
      createDefaultRow(colCode, colName, colType, colLength, isKey, dataConfig) {
        let id = randomString(32)
        return {
          id: id,
          colCode: colCode,
          colName: colName,
          colType: colType,
          colLength: colLength,
          colPointLength: 0,
          isKey: isKey ? 'Y' : 'N',
          isNull: isKey ? 'N' : 'Y',
          colDefaultVal: '',
          showType: 'hidden',
          showConfig: '',
          dataConfig: dataConfig,
          validRule: '',
          orderNum: 0,
          isDefault: 'Y',
          isRequired: 'N'
        }
      },
      handleOk() {
        const selectRecords = this.$refs.xTable.getCheckboxRecords()
        if(selectRecords.length<1){
          this.messageUtil.warning(this.$t('m.common.tip.select_one_warn'));
          return;
        }
        let exist = false;
        let existField = ""
        for(let i=0;i<selectRecords.length;i++){
          for(let j=0;j<this.oldDataSource.length;j++){
            if(selectRecords[i].colCode == this.oldDataSource[j].colCode){
              exist = true;
              existField = selectRecords[i].colCode
              break;
            }
          }
        }
        if(exist){
          this.messageUtil.warning(this.$t('m.eform.formDesign.label.zi-duan-yi-cun-zai', [existField]))
          return;
        }
        const that = this
        this.$confirm({
          title: this.$t('m.common.title.operate'),
          content: this.$t('m.common.tip.add_systemField_confirm'),
          onOk: function () {
            that.$emit("selectSystemFieldOk",selectRecords)
            that.visible = false
          }
        })

      },
    },
  }
</script>

<style lang="less">
</style>
